#!/bin/bash

# Get current Directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

#inlcude config.sh
. $DIR/config.sh

#Get Parrent Directory
Basefolder="$(cd ../; pwd)" 

# WEB SERVICES INSTALLATION
echo "Installing Node & NPM"
   if is_pizero; then
      echo "Pi 0"
	  wget https://npm.taobao.org/mirrors/node/latest-v11.x/node-v11.15.0-linux-armv6l.tar.gz
	  tar zxvf node-v11.15.0-linux-armv6l.tar.gz
	  cd node-v11.15.0-linux-armv6l/
      sudo cp -R * /usr/local/
	  sudo npm config set registry https://registry.npm.taobao.org 
      sudo npm install pm2@latest -g
     
   elif is_pione_w; then
      echo "Pi0w"
      wget -O - http://www.yyzblink.cn/install/node.sh| bash
      # Add support for node CLI tools
      export PATH=$PATH:/opt/nodejs/bin
      # install pm2 web server
      sudo npm install pm2@latest -g
      sudo ln -s /opt/nodejs/bin/pm2 /usr/bin/pm2
   else
     echo "ARMV7L"
	 wget https://registry.npmmirror.com/-/binary/node/latest-v12.x/node-v12.22.10-linux-armv7l.tar.gz
	  tar zxf node-v12.22.10-linux-armv7l.tar.gz
	  cd node-v12.22.10-linux-armv7l/
      sudo cp -R * /usr/local/
	  sudo npm config set registry https://registry.npm.taobao.org 
      sudo npm install pm2@latest -g  
   fi


cd $Basefolder/web
sudo npm install --production
sudo pm2 start process.json --env production
sudo pm2 startup
sudo pm2 save



